package com.example.lenovo.demob;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

public class StuAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Student> students;
    private IoDelStu listener;

    public StuAdapter(Context context, ArrayList<Student> students,IoDelStu listener) {
        this.context = context;
        this.students = students;
        this.listener=listener;
    }

    @Override
    public int getCount() {
        return students.size();
    }

    @Override
    public Object getItem(int i) {
        return students.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.stu_list, viewGroup, false);
            StuHolder stuHolder = new StuHolder();
            stuHolder.noview = view.findViewById(R.id.noview);
            stuHolder.nameview = view.findViewById(R.id.nameview);
            stuHolder.ageview = view.findViewById(R.id.ageview);
            stuHolder.sexview = view.findViewById(R.id.sexview);
            stuHolder.delimg=view.findViewById(R.id.delimg);
            view.setTag(stuHolder);
        }

        final Student student=students.get(i);
        StuHolder stuHolder= (StuHolder) view.getTag();
        stuHolder.noview.setText(student.getNo());
        stuHolder.nameview.setText(student.getName());
        stuHolder.ageview.setText(String.valueOf(student.getAge()));
        stuHolder.sexview.setText(student.getSex());
        stuHolder.delimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.del(student);
            }
        });

        return view;
    }
}
